//
//  BaseTabVC.m
//  ImGuider
//
//  Created by 李陆涛 on 2017/12/11.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "BaseTabVC.h"
#import "PlayerItem.h"
#import <UIScrollView+EmptyDataSet.h>

@interface BaseTabVC () <DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property (nonatomic, copy) NSString *refreshDescribe;
@property (nonatomic, copy) NSString *refreshBtnName;
@property (nonatomic, copy) NSString *refreshImageName;
@property (nonatomic, copy) void (^ refreshBlock) (void);

@end

@implementation BaseTabVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNav];
    
    if (self.isHiddenPlayStatusButton) {
        
        [[PlayerItem playItemShareManager] hide];
    }
    
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    if (self.isHiddenPlayStatusButton) {
        
        [[PlayerItem playItemShareManager] hide];
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    if (self.isHiddenPlayStatusButton) {
        
        [[PlayerItem playItemShareManager] hide];
    }
}

- (void)setAutoRowHeight {
    
    [self setAutoRowHeight:self.tableView];
}

- (void)setAutoHeadHeight {
    
    [self setAutoHeadHeight:self.tableView];
}

/**
 注册cell
 
 @param nameArray cell名字数组
 */
- (void)registCells:(NSArray *)nameArray {
    
    [self registCells:nameArray tableView:self.tableView];
}


#pragma mark - getter setter
- (void)setDataHasLoad:(BOOL)dataHasLoad {
    
    _dataHasLoad = dataHasLoad;
    if (dataHasLoad) {
        self.tableView.emptyDataSetSource = self;
        self.tableView.emptyDataSetDelegate = self;
        [self.tableView reloadEmptyDataSet];
    }
}

#pragma mark - 配置空数据页面
- (void)configEmptyDataWithEmptyImageName:(NSString *)imageName
                                 describe:(NSString *)describe
                                  refresh:(NSString *)btnName
                                    block:(void (^) (void))block {
    
    self.refreshImageName = imageName;
    self.refreshDescribe = describe;
    self.refreshBtnName = btnName;
    self.refreshBlock = block;
}

#pragma mark - DZNEmptyDataSetSource Methods

//- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
//
//    NSString *text = @"暂无数据";
//    return [[NSAttributedString alloc] initWithString:text attributes:nil];
//}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    
    NSString *text = [AppManager sharedManager].networkState == AFNetworkReachabilityStatusNotReachable ? MYLocalizedString(@"PUBLIC_NETWORK_ERROR", nil) : self.refreshDescribe;// @"暂无收藏导游\n发现有趣的就收藏吧~";
    UIFont *font = Font(14);
    UIColor *textColor = [UIColor grayColor];
    
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    [attributes setObject:font forKey:NSFontAttributeName];
    [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
    paragraphStyle.alignment = NSTextAlignmentCenter;
    [attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
    
    return [UIColor groupTableViewBackgroundColor];
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    
    return -kNavigationBarHeight;
}

- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView {
    
    return 50;
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    
    if ([AppManager sharedManager].networkState == AFNetworkReachabilityStatusNotReachable) {
        return nil;
    }
    
    NSString *text = self.refreshBtnName;
    
    if (text.length == 0) {
        return nil;
    }
    
    UIFont *font = Font(14);
    UIColor *textColor = kThemeColor;
    
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    [attributes setObject:font forKey:NSFontAttributeName];
    [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    
    return [AppManager sharedManager].networkState == AFNetworkReachabilityStatusNotReachable ? [UIImage imageNamed:@"网络不好"] : [UIImage imageNamed:self.refreshImageName];
}

#pragma mark - DZNEmptyDataSetDelegate Methods

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    
    return YES;
}

//- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
//
//    return YES;
//}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    
    return YES;
}

//- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
//
//    IGLog(@"%s",__FUNCTION__);
//}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
    
    if (self.refreshBlock) {
        self.refreshBlock();
    }
}

@end

